package com.upload.controller;

import com.upload.bean.yCoin.LoginInfoBean;
import com.upload.bean.yCoin.YcoinRequestBean;
import com.upload.bean.yCoin.YcoinResponseBean;
import com.upload.domain.Document;
import com.upload.enums.Status;
import com.upload.jms.audited.AuditToYcoinClient;
import com.upload.repository.DocumentRepository;
import com.upload.util.CookieUtil;
import com.upload.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.JMSException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by huangying on 2017/3/30.
 */
@RequestMapping(value = "/api")
@RestController
public class TestController {

    @Autowired
    private AuditToYcoinClient auditToYcoinClient;

    @Autowired
    private DocumentRepository documentRepository;

    @RequestMapping("a")
    public String a(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort()+request.getRequestURI()+"?"+request.getQueryString());

        System.out.println("requestUri = " + request.getRequestURI());
        System.out.println("requestQueryString = " + request.getQueryString());
        System.out.println("requestServletPath = " + request.getServletPath());
        System.out.println("requestContextPath = " + request.getContextPath());
        System.out.println("requestRemoteUser = " + request.getRemoteUser());

        CookieUtil.setCookie("hy", "66666666666", 15, response);
        return "aaaaa";
    }

    @RequestMapping("b")
    public String b(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort()+request.getRequestURI()+"?"+request.getQueryString());

        return CookieUtil.getCookie(request, "hy");
    }

    @RequestMapping(value = "c", method = RequestMethod.POST)
    public String c(HttpServletResponse response) {
        CookieUtil.setCookie("c", "cccccccc", 15, response);
        return "ccccc";
    }

    @RequestMapping(value = "l", method = RequestMethod.GET)
    public LoginInfoBean loadResponse(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response){

        System.out.println(request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort()+request.getRequestURI()+"?"+request.getQueryString());
        System.out.println("login");
        System.out.println("body = " + code);

        System.out.println("requestUri = " + request.getRequestURI());
        System.out.println("requestQueryString = " + request.getQueryString());
        System.out.println("requestServletPath = " + request.getServletPath());
        System.out.println("requestContextPath = " + request.getContextPath());
        System.out.println("requestRemoteUser = " + request.getRemoteUser());

        LoginInfoBean loginInfoBean = new LoginInfoBean();

        loginInfoBean.setStatus("103");
        loginInfoBean.setMsg("用户未注册，请先关注服务号");
        loginInfoBean.setUnionid("i-am-unionid");

        System.out.println(JSONUtil.toJsonStr(loginInfoBean));

        return loginInfoBean;
    }

    @RequestMapping(value="/jms")
    public String testlogin(){

        //通知逸贝系统加逸贝
        YcoinRequestBean ycoinRequestBean = new YcoinRequestBean("opkdCwNSzMVKj_KhyBZIMdSKhSVc");
        ycoinRequestBean.setCheckUnionid("opkdCwKjuTunONfOWeYpB9Qe_iMQ");
        ycoinRequestBean.setCheckDate(new Date().getTime());
        ycoinRequestBean.setyCoin(101);
        YcoinResponseBean ycoinResponseBean = null;
        try {
            ycoinResponseBean = auditToYcoinClient.sender(ycoinRequestBean);
            System.out.println("ycoinResponseBean---------------"+ycoinResponseBean.toString());
        } catch (JMSException e) {
            e.printStackTrace();
        }
        if (ycoinResponseBean.isSuccess()) {
            Document document = new Document();
            document.setyCoin(101);
            document.setStatus(Status.audited);
            documentRepository.save(document);
            return "yes";
        }
        return "no";
    }
}
